package net.onceface.core.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import net.onceface.core.domain.enums.OrderStatus;
import net.onceface.core.domain.enums.Payment;
import net.onceface.infra.domain.AbstractEntity;
import net.onceface.infra.domain.CriteriaQuery;

@Entity
@Table(name = "SYS_ORDER")
public class Order extends AbstractEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7311763714658286772L;

	/**
	 * 订单编号
	 * **/
	@Column(name = "ORDER_NUMBER", nullable = false , unique = true)
	private String orderNumber;
	
	/**
	 * 支付方式
	 * **/
	@Enumerated(EnumType.STRING)
	@Column(name = "PAYMENT")
	private Payment payment;
	
	/**
	 * 订单总金额
	 * **/
	@Column(name = "TOTAL_PRICE")
	private double totalPrice;
	
	/**
	 * 订单优惠金额
	 * **/
	@Column(name = "DISCOUNT")
	private double discount;
	
	/**
	 * 订单金额
	 * **/
	@Column(name = "PRICE")
	private double price;
	
	/**
	 * 订单使用的优惠券ID
	 * 字符串类型，多张用，(逗号)隔开
	 * **/
	@Column(name = "COUPONS_ID")
	private String couponsId;
	
	/**
	 * 订单的优惠信息ID
	 * **/
	@Column(name = "DISCOUNT_ID")
	private Long disCountId;
	
	/**
	 * 用户ID
	 * **/
	@Column(name = "CLIENT_ID")
	private Long clientId;
	
	/**
	 * 订单状态
	 * **/
	@Enumerated(EnumType.STRING)
	@Column(name = "ORDER_STATUS")
	private OrderStatus orderStatus;
	
	/**
	 * 快递单号
	 * **/
	@Column(name = "EXPRESS_NUMBER")
	private String expressNumber;
	
	/**
	 * 快递名称
	 * **/
	@Column(name = "EXPRESS_NAME")
	private String expressName;
	
	/**
	 * 备注
	 * **/
	@Column(name = "REMARKS")
	private String remarks;
	
	/**
	 * 收货地址
	 * **/
	@Column(name = "ADDRESS")
	private String address;
	
	/**
	 * 收货人
	 * **/
	@Column(name = "RECEIVER")
	private String receiver;
	
	/**
	 * 收货人号码
	 * **/
	@Column(name = "PHONE")
	private String phone;
	
	/**
	 * 订单商品
	 * **/
	@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.MERGE ,CascadeType.REMOVE})
	@JoinColumn(name = "ORDER_ID", referencedColumnName = "ID")
	private List<OrderProduct> orderProducts;
	
	/**
	 * 创建日期
	 * **/
	@Column(name = "CREATE_DATE", nullable = false ,updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createDate;
	
	/**
	 * 根据订单编号查找订单
	 * **/
	public static Order getOrderByOrderNumber(String orderNumber){
		CriteriaQuery c = new CriteriaQuery(getRepository(), Order.class);
		c.eq("orderNumber", orderNumber);
		return getRepository().getSingleResult(c);
	}
	
	public Order(){}
	
	public Order(Long clientId, OrderStatus orderStatus){
		this.createDate = new Date();
		this.clientId = clientId;
		this.orderStatus = orderStatus;
	}
	
	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public Long getDisCountId() {
		return disCountId;
	}

	public void setDisCountId(Long disCountId) {
		this.disCountId = disCountId;
	}

	public Payment getPayment() {
		return payment;
	}

	public void setPayment(Payment payment) {
		this.payment = payment;
	}

	public double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getCouponsId() {
		return couponsId;
	}

	public void setCouponsId(String couponsId) {
		this.couponsId = couponsId;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public OrderStatus getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(OrderStatus orderStatus) {
		this.orderStatus = orderStatus;
	}

	public String getExpressNumber() {
		return expressNumber;
	}

	public void setExpressNumber(String expressNumber) {
		this.expressNumber = expressNumber;
	}

	public String getExpressName() {
		return expressName;
	}

	public void setExpressName(String expressName) {
		this.expressName = expressName;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public List<OrderProduct> getOrderProducts() {
		return orderProducts;
	}

	public void setOrderProducts(List<OrderProduct> orderProducts) {
		this.orderProducts = orderProducts;
	}

	@Override
	public String[] businessKeys() {
		// TODO Auto-generated method stub
		return null;
	}

}
